﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ServiceDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        else
        {
            if (Request.QueryString["SubCategoryID"] != null)
            {
                PostBackPage();
            }
        }
    }

    private void LoadData()
    {
        try
        {
            int id = Int32.Parse(Request.QueryString["ServiceCategoryID"]);
            ManagementService processService = new ManagementService();
            ManagemetSubService processSub = new ManagemetSubService();
            DataSet ds = processSub.GetSubCategoryByCategoryID(id);
            DataSet dsName = processService.GetService();

            int subID = Int32.Parse(ds.Tables[0].Rows[0].ItemArray[0].ToString());

            parentRepeater.DataSource = dsName;
            parentRepeater.DataBind();

            FlagMenuLeft(id, subID);
            lbTitlePayment.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString();
            Image1.ImageUrl = "images/" + ds.Tables[0].Rows[0].ItemArray[3].ToString();
            lbDescription.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
            LoadDataGripView(subID);
        }
        catch (Exception)
        {
            ErrorReturnHome();
        }
    }

    private void PostBackPage()
    {
        try
        {
            int ServiceCategoryID = Int32.Parse(Request.QueryString["ServiceCategoryID"]);
            int SubCategoryID = Int32.Parse(Request.QueryString["SubCategoryID"]);
            FlagMenuLeft(ServiceCategoryID, SubCategoryID);
            ManagemetSubService processSub = new ManagemetSubService();
            DataSet ds = processSub.GetSubcategoryByID(SubCategoryID);

            lbTitlePayment.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString();
            Image1.ImageUrl = "images/" + ds.Tables[0].Rows[0].ItemArray[3].ToString();
            lbDescription.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
            LoadDataGripView(SubCategoryID);
        }
        catch (Exception)
        {
            ErrorReturnHome();
        }
    }

    private void FlagMenuLeft(int ServiceCategoryID,int SubCategoryID)
    {
        menu_active1.Value = ServiceCategoryID.ToString();
        menu_active2.Value = SubCategoryID.ToString();
    }

    private void LoadDataGripView(int subCategoryID)
    {
        ManagementPayment process = new ManagementPayment();
        gvPaymentMethod.DataSource = process.GetPaymentMethodBySubCategoryID(subCategoryID);
        gvPaymentMethod.DataBind();
    }

    private void ErrorReturnHome()
    {
        Response.Redirect("Home.aspx");
    }


    private bool CheckPaymentType(int subCategoryID)
    {
        ManagemetSubService process = new ManagemetSubService();
        int result = process.CheckTypeSubCategory(subCategoryID);
        return result > 0;
    }

    protected void gvPaymentMethod_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Response.Redirect("Contract.aspx?PaymentID=" + gvPaymentMethod.DataKeys[e.NewSelectedIndex].Value);
    }

    protected void parentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ManagemetSubService process = new ManagemetSubService();
            Repeater childRepeater = (Repeater)e.Item.FindControl("childRepeater");
            childRepeater.DataSource = process.GetDataSubcategoryByServiceID("SELECT SubName,SubCategoryID,ServiceCategoryID FROM SubCategory where ServiceCategoryID=" + ((System.Data.DataRowView)(e.Item.DataItem)).Row[0]);
            childRepeater.DataBind();
        }
    }
}